//
//  DetailsViewController.m
//  tuanGG
//
//  Created by 微他 on 15/9/23.
//  Copyright (c) 2015年 weita. All rights reserved.
//

#import "DetailsViewController.h"
#import "Header.h"
#import "ImageView.h"
#import "ModifyPhoneViewController.h"
@interface DetailsViewController ()<UIActionSheetDelegate,UITableViewDataSource,UITableViewDelegate,ImageViewDelegate>
@property (nonatomic,strong)UITextField *NewTextField;
@property (nonatomic,strong)UITextField *oldTextField;
@property (nonatomic,strong)UITextField *textField;
@property (nonatomic,strong)UIButton *saveButton;
@property (nonatomic,assign)NSInteger index;
@property (nonatomic,strong)UIButton *Verification;     //验证码
@property (nonatomic,strong)NSDictionary *dictionary;   //网络请求参数
@property (nonatomic,strong)NSString *netAddress;       //网络地址
@property (nonatomic,strong)UITextView *textView;       //购买须知
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSArray *typeArray;
@property (nonatomic,strong)ImageView *imgae;
@property (nonatomic,strong)UIScrollView * scrollerview;

@property (nonatomic,strong)NSMutableArray * normalArray;
@property (nonatomic,strong)NSMutableArray * imgArray;//所选择的图片存储容器
@property (nonatomic,strong)NSMutableArray * editImgArray;//编辑后的图片
@property (nonatomic,strong)NSMutableArray * editArrayNum;//存在图片的编号
@property (nonatomic,strong)UIView * auxiliaryView;
@property (nonatomic,strong)UIButton *addButton;//添加图片
@end

@implementation DetailsViewController
- (void)viewWillAppear:(BOOL)animated{
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:font_Font size:20],
                                                                      NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.barTintColor =[UIColor colorWithHexString:@"4fd2be"];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initdata];
    self.view.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
    [self initView];
    if ([_string isEqualToString:@"店铺名称"]) {
        [self storeName];
    }else if ([_string isEqualToString:@"产品介绍"]){
        
        [self ProductIntroduction];
        
    }else if([_string isEqualToString:@"购买须知"]){
        
        [self PurchaseInformation];
        
    }else if([_string isEqualToString:@"客服电话"]){
        
        [self serviceTelephone];
        
    }else if([_string isEqualToString:@"所在地区"]){
        
        
    }else if([_string isEqualToString:@"详细地址"]){
        
        [self detailedAddress];
        
    }else if([_string isEqualToString:@"已绑定手机"]){
        
        [self bindingPhone];
        
    }else if([_string isEqualToString:@"登录密码"]){
        [self loginPassword];
    }else if ([_string isEqualToString:@"店铺类型"]){
        [self storeType];
    }

}

- (void)initdata
{

    _normalArray = [[NSMutableArray alloc]init];
    _imgArray = [[NSMutableArray alloc]init];
    _editImgArray = [[NSMutableArray alloc]init];
    _editArrayNum = [[NSMutableArray alloc]init];
}
- (void)initView {
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth, 50)];
    _textField.backgroundColor  =[UIColor whiteColor];
    _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
    _textField.leftViewMode =  UITextFieldViewModeAlways;
    [self.view addSubview:_textField];
    _saveButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth-20, 50)];
    _saveButton.center = CGPointMake(ScreenWidth/2, _textField.frame.origin.y+150);
    _saveButton.backgroundColor = [UIColor colorWithHexString:@"4fd2be"];
    [_saveButton setTitle:@"保存" forState:UIControlStateNormal];
    _saveButton.layer.cornerRadius = 5;
    [_saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_saveButton addTarget:self action:@selector(saveButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_saveButton];

    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource =self;
    _tableView.hidden = YES;
    _tableView.bounces = NO;
    _tableView.backgroundColor = UIColorFromRGB(base_gary_bg);
    _tableView.tableFooterView = [[UITableView alloc]init];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:_tableView];

    
}
//商铺名称
- (void)storeName{
    _textField.placeholder = @"请输入商家名称（仅限8个字）";
    _saveButton.tag = 0;
}
//店铺类型
- (void)storeType{
    _textField.hidden = YES;
    _saveButton.hidden = YES;
    _tableView.hidden = NO;
    _typeArray = @[@"火锅",@"中餐",@"西餐",@"汤锅",@"干锅烤鱼",@"快餐",@"小吃",@"烧烤烤肉",@"日韩料理",@"休闲娱乐"];
}
//产品介绍
- (void)ProductIntroduction{
    _textField.hidden = YES;
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth, 150)];
    lineView.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
    label.text = @"  上传您的产品图片（不超过15张）";
    label.textColor = [UIColor colorWithWhite:0.702 alpha:1.000];
    for (int i = 0; i < 2; i++) {
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, i*label.frame.size.height, ScreenWidth, 1)];
        line.backgroundColor = [UIColor blackColor];
        line.alpha = 0.2;
        [label addSubview:line];
    }
    
    [lineView addSubview:label];
    [self.view addSubview:lineView];
    _saveButton.center = CGPointMake(ScreenWidth/2, lineView.frame.size.height+120);
    _saveButton.tag = 1;
    
    _scrollerview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 60, ScreenWidth, 80)];
    _scrollerview.contentSize = CGSizeMake(ScreenWidth*3, 0);
    _scrollerview.userInteractionEnabled = YES;
//    _scrollerview.backgroundColor = [UIColor redColor];
//    _scrollerview.showsVerticalScrollIndicator = NO;
    [lineView addSubview:_scrollerview];
    
    _addButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 10, 80, 60)];
    [_addButton setImage:[UIImage imageNamed:@"add_goodsImage"] forState:UIControlStateNormal];
    [_addButton addTarget:self action:@selector(addImageButton) forControlEvents:UIControlEventTouchUpInside];
    [_scrollerview addSubview:_addButton];
    
    //辅助加载图片
    _auxiliaryView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 10)];
    
    [_scrollerview addSubview:_auxiliaryView];
    
    
//先判断是否有产品的图片
    NSArray * array = [[User_member instance].pics componentsSeparatedByString:@","];
    for (int i = 0; i < array.count; i ++) {
        
        if (array[i] != nil && ![array[i] isEqualToString:@""]) {
            [_imgArray addObject:array[i]];
            [_editImgArray addObject:array[i]];
            [_editArrayNum addObject:[NSString stringWithFormat:@"%d",i+1]];
        }
    }
     _addButton.frame = CGRectMake(20+_imgArray.count * 90, 10, 80, 60);
    [_scrollerview setContentOffset:CGPointMake((_imgArray.count-1) * 60,0) animated:YES];
    [self groupView:_imgArray view:_auxiliaryView superView:_scrollerview photoButttonArray:_normalArray index_i:1 index_j:_imgArray.count itemWidth:109];
    
}
//购买须知
- (void)PurchaseInformation{
    _textView =[[UITextView alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth, 200)];
    _textView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_textView];
//    _textField.frame = CGRectMake(0, 20, ScreenWidth, 200);
//    _textField.placeholder = @"提醒客户哪些需要注意...";
    _saveButton.center = CGPointMake(ScreenWidth/2, _textView.frame.size.height+120);
    _saveButton.tag = 2;
}
//客服电话
- (void)serviceTelephone{
    _textField.placeholder = @"请输入客服电话";
    _saveButton.tag = 3;
}
//所在地区
- (void)inTheArea{
    NSLog(@"");
    _saveButton.tag = 4;
}
//详细地址
- (void)detailedAddress{
    _textField.placeholder = @"请输入详细地址";
    _saveButton.tag = 5;
}
//已绑定手机
- (void)bindingPhone {
    NSArray *array = @[@"",@"请输入验证码"];
    for (int i = 0; i < 2; i++) {
        _textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 20+i*51, ScreenWidth, 50)];
        _textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
        _textField.leftViewMode =  UITextFieldViewModeAlways;
        _textField.placeholder = array[i];
        _textField.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_textField];
        if (i == 0) {
            NSString *str1 = [[UserInfo sharedUserInfo].phoneNumber substringToIndex:3];
            NSString *str2 = [[UserInfo sharedUserInfo].phoneNumber substringFromIndex:7];

            NSString *str = [NSString stringWithFormat:@"%@****%@",str1,str2];
            _textField.text = str;
            _textField.userInteractionEnabled = NO;
            _textField.rightViewMode = UITextFieldViewModeAlways;
            _Verification = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-70, _textField.frame.size.height/2+5, 60, 30)];
            [_Verification setTitle:@"获取验证码" forState:UIControlStateNormal];
            [_Verification setTitleColor:[UIColor colorWithHexString:@"33ccbf"] forState:UIControlStateNormal];
            [_Verification addTarget:self action:@selector(verificationCode) forControlEvents:UIControlEventTouchUpInside];
            _Verification.titleLabel.numberOfLines = 2;
            _Verification.layer.cornerRadius = 5;
            _Verification.layer.borderWidth = 1;
            _Verification.layer.borderColor = [[UIColor colorWithHexString:@"33ccbf"]CGColor];
            _Verification.titleLabel.font = BOLD_FONT(11);
            [self.view addSubview:_Verification];
        }

    }
    [_saveButton setTitle:@"下一步" forState:UIControlStateNormal];
    _saveButton.center = CGPointMake(ScreenWidth/2, _textField.frame.size.height*2+120);
    _saveButton.tag = 6;
}
//登录密码
- (void)loginPassword{
    _textField.hidden = YES;
    NSArray *array = @[@"当前密码",@"新密码",@"确认新密码"];
    for (int i = 0; i < 3; i++) {
        if (i == 0) {
            _oldTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth, 50)];
            _oldTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
            _oldTextField.leftViewMode =  UITextFieldViewModeAlways;
            _oldTextField.placeholder = array[i];
            _oldTextField.secureTextEntry = YES;
            _oldTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
            _oldTextField.backgroundColor = [UIColor whiteColor];
            [self.view addSubview:_oldTextField];
        }else if(i == 1){
            _NewTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, 20+i*51, ScreenWidth, 50)];
            _NewTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
            _NewTextField.leftViewMode =  UITextFieldViewModeAlways;
            _NewTextField.placeholder = array[i];
            _NewTextField.secureTextEntry = YES;
            _NewTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
            _NewTextField.backgroundColor = [UIColor whiteColor];
            [self.view addSubview:_NewTextField];
        }else{
            _textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 20+i*51, ScreenWidth, 50)];
            _textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
            _textField.leftViewMode =  UITextFieldViewModeAlways;
            _textField.placeholder = array[i];
            _textField.secureTextEntry = YES;
            _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            _textField.backgroundColor = [UIColor whiteColor];
            [self.view addSubview:_textField];
        }
    }
    _saveButton.center = CGPointMake(ScreenWidth/2, _textField.frame.size.height*3+120);
    _saveButton.tag = 7;
}
#pragma mark - 事件实现
- (void)saveButton:(UIButton *)sender {
    if (sender.tag == 0) {
        _dictionary = @{@"merchname":_textField.text};
        _netAddress = alterMydataName_url;
        _index = 7;
        [self netWorking];
    }else if (sender.tag == 1) {
//        [self imageNetworking];
        if (_imgArray.count == 0) {
            [Common addAlert:@"请至少上传一张店铺照片"];
        }else {
            [self imageNetworking];
        }
        NSLog(@"产品介绍");
    }else if (sender.tag == 2) {
        _dictionary = @{@"needtoknow":_textView.text};
        _netAddress = alterMyInstructions_url;
        _index = 7;
        [self netWorking];
    }else if (sender.tag == 3) {
        if (_textField.text.length <1) {
            [Common addAlert:@"请输入客服电话"];
        }else if (![ValidateMobileWithEmail validateMobile:_textField.text]) {
            [Common addAlert:@"手机号码格式不正确"];
        }else {
        _dictionary = @{@"helptelephone":_textField.text};
        _netAddress = alterMydataServerNum_url;
        _index = 7;
        [self netWorking];
        }

    }else if (sender.tag == 4) {
        NSLog(@"所在地区");
    }else if (sender.tag == 5) {
        if (_textField.text.length <1) {
            [Common addAlert:@"请输入您的详细地址"];
        }else{
            _dictionary = @{@"detail":_textField.text,@"jingdu":@"0",@"weidu":@"0"};
            _netAddress = detail_url;
            _index = 7;
           // [self.navigationController popViewControllerAnimated:YES];
            [self netWorking];
        }
    }else if (sender.tag == 6) {
        NSLog(@"已绑定手机");
        if (_textField.text.length !=4) {
            [Common addAlert:@"请输入4位数的验证码"];
        }else {
            _dictionary = @{@"newphone":[UserInfo sharedUserInfo].phoneNumber,@"vcode":_textField.text};
            _netAddress = alterMydataPhone_url;
            _index = 6;
            ModifyPhoneViewController *modify = [[ModifyPhoneViewController alloc]init];
            [self.navigationController pushViewController:modify animated:YES];
//            [self netWorking];
        }

    }else if (sender.tag == 7) {
        if (_oldTextField.text.length <= 0) {
            [Common addAlert:@"请输入原始密码"];
        }else if([_oldTextField.text isEqualToString:_textField.text]){
            [Common addAlert:@"输入的密码与当前密码不能一致"];
        }else if(_NewTextField.text.length <= 0){
            [Common addAlert:@"请输入新密码"];
        }else if (![_textField.text isEqualToString:_NewTextField.text]){
            [Common addAlert:@"新密码前后不一致"];
        }else{
            _dictionary = @{@"newpwd":_NewTextField.text,@"oldpwd":_oldTextField.text};
            _netAddress = alterMydatapsw_url;
            _index = 7;
            [self netWorking];
        }
    }
}

//添加产品图片
- (void)addImageButton{
    
    
    if (_imgArray.count < 15) {
        UIActionSheet *choiceSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                                 delegate:self
                                                        cancelButtonTitle:@"取消"
                                                   destructiveButtonTitle:@"拍照"
                                                        otherButtonTitles:@"相册", nil];
        [choiceSheet showInView:self.view];
    }
    else
    {
       [Common addAlert:@"最多上传15张"];
    }
    
}
//获取验证码
- (void)verificationCode{
    _index = 0;
    [CustomAlertView alterMsg:[NSString stringWithFormat:@"是否是通过%@号码获取验证码吗?",[UserInfo sharedUserInfo].phoneNumber] delegate:self];


}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}
#pragma mark - 网络请求
- (void)netWorking{
//    NSLog(@"%@",_dictionary);

 [GLAFRequest startRequest:_netAddress paramsDic:_dictionary defaultdialog:YES ispost:YES handleSuccess:^(id sta, NSDictionary *paramsDic) {
     NSLog(@"信息：%@",sta);
     if ([sta[@"code"]integerValue] == 1) {
         if (_typeBlock) {
             _typeBlock(_typeArray);
             [self.navigationController popViewControllerAnimated:YES];
         }
         if( _index == 7){
             [self addAlert:@"修改成功" otherButtonTitles:nil];
             if(sta[@"msg"]){
                 [Common addAlert:@"密码输入不正确"];
             }
         }else if (_index == 6) {
             ModifyPhoneViewController *modify = [[ModifyPhoneViewController alloc]init];
             [self.navigationController pushViewController:modify animated:YES];
         }

     }

     }doFinally:^(NSDictionary *paramsDic) {
 }];
}






- (void)imageNetworking {
    
    
    NSString *picString = @"pic1";
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    NSMutableDictionary * imgdic = [NSMutableDictionary dictionary];
    //上传图片
    for (int i=0; i < _imgArray.count; i ++) {
        if (i > 0) {
            picString = [picString stringByAppendingFormat:@",pic%d",i+1];
        }
        if ([_imgArray[i] isKindOfClass:[UIImage class]]) {
            
            
            //            UIImage * image = [UIImage imageWithContentsOfFile:_detailDataImage[n]];
            [imgdic setObject:_imgArray[i] forKey:[NSString stringWithFormat:@"pic%d",i+1]];
        }
        else
        {
            
            [params setObject:_editArrayNum[i] forKey:[NSString stringWithFormat:@"pic%d",i+1]];
        }
    }
    if (_imgArray.count > 0) {
        [params setObject:picString forKey:@"pics"];
    }


    [GLAFRequest startFileRequest:alterGoodsIm_url time:300 isTouch:NO paramsDic:params defaultdialog:YES picDic:imgdic handleSuccess:^(id sta, NSDictionary *paramsDic) {
        if ([sta[@"code"] integerValue] ==1) {
            
            [Common addAlert:@"产品图片上传成功"];
            
            [_editArrayNum removeAllObjects];
            [_imgArray removeAllObjects];
            NSArray * array = [sta[@"a_memberupdateobj"][@"pics"] componentsSeparatedByString:@","];
            for (int i = 0; i < array.count; i ++) {
                
                if (array[i] != nil && ![array[i] isEqualToString:@""]) {
                    [_imgArray addObject:array[i]];
//                    [_editImgArray addObject:array[i]];
                    [_editArrayNum addObject:[NSString stringWithFormat:@"%d",i+1]];
                }
            }
            
            
        }
    } netWorkFail:^(NSDictionary *paramsDic) {
        
    }];
}
#pragma mark -UIAlertViewdelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0 && _index == 7) {
        [self.navigationController popViewControllerAnimated:YES];
    }if (buttonIndex == 0 && _index == 0) {
//        NSLog(@"验证码");
        [self sendVerification];
    }

}
//获取验证码
- (void)sendVerification{
    [Common verificationCode:^{
        [_Verification setTitle:@"获取验证码" forState:UIControlStateNormal];
        _Verification.userInteractionEnabled = YES;
    } blockNo:^(id time) {
        [_Verification setTitle:[NSString stringWithFormat:@"获取验证码\n     %@'s",time] forState:UIControlStateNormal];
        NSLog(@"%@",_Verification.titleLabel.text);
        _Verification.userInteractionEnabled = NO;
        //发送验证
    }];

    //发送验证信息
//    [SMS_SDK getVerificationCodeBySMSWithPhone:[UserInfo sharedUserInfo].phoneNumber zone:@"86"   result:^(SMS_SDKError *error) {
//
//        if (!error) {
//
//            NSLog(@"");
//        }
//        else
//        {
//            NSLog(@"错误:%@",error.description);
//            [CustomAlertView alterSureMsg:[NSString stringWithFormat:@"%@",error.description] delegate:nil];
//        }
//
//    }];
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:[UserInfo sharedUserInfo].phoneNumber zone:@"86" customIdentifier:nil result:^(NSError *error) {
        if (!error) {
            [Common addAlert:@"发送成功"];
        }else {
            [Common addAlert:@"发送失败"];
        }
    }];
}
#pragma mark - <UIActionSheetDelegate>

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            WJPhotoTool *wj = [WJPhotoTool sharedWJPhotoTool];
            [wj WJUIImagePiker:self type:1 needCut:YES block:^(UIImage *edtingImage, UIImage *originalImage) {
                
                [self AddImagesMethod:edtingImage];
              
            }];
        }
    }if (buttonIndex == 0) {
            WJPhotoTool *wj = [WJPhotoTool sharedWJPhotoTool];
            [wj WJUIImagePiker:self type:0 needCut:YES block:^(UIImage *edtingImage, UIImage *originalImage) {
                [self AddImagesMethod:edtingImage];
//                [_imgArray addObject:edtingImage];
            }];

    }
}
#pragma mark -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _typeArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    cell.textLabel.text = _typeArray[indexPath.row];
    return cell;
}

//店铺类型选择
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"%ld",indexPath.row+1);
    _netAddress = goodsType;
    _dictionary = @{@"type":[NSString stringWithFormat:@"%zi",indexPath.row+1]};
    [self netWorking];
}


#pragma mark - <商品图片的添加>
- (void)groupView:(NSArray *)array view:(UIView *)view superView:(UIView *)superView photoButttonArray:(NSMutableArray *)photoButttonArray index_i:(NSInteger)index_i index_j:(NSInteger)index_j itemWidth:(NSInteger)itemWidth{
    
    NSInteger space = 20*PRO;
    NSInteger width = itemWidth *PRO - space;
    NSInteger height = 69;
    //先将UIButton清空
    for (UIImageView *button in photoButttonArray) {
        [button removeFromSuperview];
    }
    //得到数组数量
    NSInteger buttonCount = array.count;
    NSInteger index = 0;
    
    //        NSInteger index_i = 3;
    //        NSInteger index_j = 4;
    for (int i = 0 ; i < index_i; i++) { //heng
        for (int j = 0; j < index_j; j++) { // shu
            
            _imgae = [[ImageView alloc]initWithFrame:CGRectMake(10+space/4 + width *j, CGRectGetMaxY(view.frame)+ 4 + i * width+i*space/2 -5, width, height)];
//            _imgae.backgroundColor= [UIColor purpleColor];
            _imgae.userInteractionEnabled = YES;
            _imgae.isshowDeleteImage = YES;
            _imgae.delegate = self;
            
            //            _imgae.backgroundColor = [UIColor redColor];
            [superView addSubview:_imgae];
            if (index < array.count) {
                
                if ([array[index] isKindOfClass:[UIImage class]]) {
                    _imgae.image.image = array[index];
                }
                else
                {
                    if ([[array[index] substringToIndex:4] isEqualToString:@"http"]) {
                        
                        
                        [_imgae.image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",array[index]]] placeholderImage:nil];
                        //                        //网络图片存储
                        //                        [_netimageArray addObject:_imgae.image];
                    }
                }
            }
            _imgae.userInteractionEnabled = YES;
            _imgae.tag = 10 + index;
            _imgae.index = 10 + index;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTapPressed:)];
            [_imgae addGestureRecognizer:tap];
            [photoButttonArray addObject:_imgae];
            if (index == buttonCount) {
                _imgae.image.image = [UIImage imageNamed:@"add_goodsImage"];
                _imgae.tag = 200;
                //得到图片最下面高度
                //                _imagePhotoHeight=CGRectGetMaxY(image.frame) +10 - width;
            }
            if (index > buttonCount) {
                [_imgae removeFromSuperview];
            }
            if (index >8) {
                [_imgae removeFromSuperview];
            }
            index ++;
        }
        
    }
}
- (void)imageTapPressed:(UITapGestureRecognizer *)tap
{
    NSLog(@"%ld",tap.view.tag);
    
//    [self addImageButtonCilck];
}

#pragma mark - <ImageViewDelegate>
- (void)selectImage:(NSInteger)indexPath
{
    //选择的是哪一张图片
    if (![_imgArray[indexPath-10] isKindOfClass:[UIImage class]]) {

      [_editArrayNum removeObjectAtIndex:indexPath-10];
    }
    [_imgArray removeObjectAtIndex:indexPath-10];

    _addButton.frame = CGRectMake(20+_imgArray.count * 90, 10, 80, 60);
    [_scrollerview setContentOffset:CGPointMake((_imgArray.count-1) * 60,0) animated:YES];
    [self groupView:_imgArray view:_auxiliaryView superView:_scrollerview photoButttonArray:_normalArray index_i:1 index_j:_imgArray.count itemWidth:109];
    
}

- (void)AddImagesMethod:(UIImage *)imgs
{
      [_imgArray addObject:imgs];
    
    _addButton.frame = CGRectMake(20+_imgArray.count * 90, 10, 80, 60);
    [_scrollerview setContentOffset:CGPointMake((_imgArray.count-1) * 60,0) animated:YES];
     [self groupView:_imgArray view:_auxiliaryView superView:_scrollerview photoButttonArray:_normalArray index_i:1 index_j:_imgArray.count itemWidth:109];
}

@end
